package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.LoginEvent;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.contract.LoginContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.presenter.LoginPresenter;
import tongwentongshu.com.app.utils.SpUtils;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/11.
 */

public class LoginActivity extends BaseActivity implements LoginContract.View {
    @BindView(R.id.et_login_phone)
    EditText etPhone;
    @BindView(R.id.et_login_password)
    EditText etPassword;

    LoginPresenter loginPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_login;
    }

    @Override
    protected int getTitleId() {
        return R.string.login;
    }

    @Override
    protected void initView() {
        String phone = SpUtils.getString(mContext, Cache.USER_NAME, "-1");
        String password = SpUtils.getString(mContext, Cache.USER_PASSWORD, "-1");
        if (!phone.equals("-1"))
            etPhone.setText(phone);
        if (!password.equals("-1"))
            etPassword.setText(password);
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this);
    }

    @OnClick({R.id.text_to_register, R.id.text_forget_pass, R.id.login_btn, R.id.tv_later_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_to_register:
                Bundle b = new Bundle();
                b.putString(Cache.REGISTER_KEY, Cache.REGISTER_VERIFICATION);
                startActivity(RegisterActivity.class, b);
                break;
            case R.id.text_forget_pass:
                startActivity(BackPasswordActivity.class);
                break;
            case R.id.login_btn:
                loginPresenter.login(mContext);
                break;
            case R.id.tv_later_login:
                if (Cache.getUser().isLogin()) {
                    Cache.getUser().getData().setToken("");
                    Cache.getUser().setLogin(false);
                }
                finish();
                break;
        }
    }

    @Override
    public String getPw() {
        return etPassword.getText().toString().trim();
    }

    @Override
    public String getPhone() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public void onSuccess(String msg) {
//        ToastUtil.show(mContext, msg);
//        EventBus.getDefault().post(new LoginEvent());
        finish();
    }

    /**
     * 监听Back键按下事件,方法2:
     * 注意:
     * 返回值表示:是否能完全处理该事件
     * 在此处返回false,所以会继续传播该事件.
     * 在具体项目中此处的返回值视情况而定.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (Cache.getUser().isLogin()) {
                Cache.getUser().getData().setToken("");
                Cache.getUser().setLogin(false);
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }

    @Override
    public void onShowError(String msg) {
        ToastUtil.show(mContext, msg);
    }
}
